package JdbcTemplate.operation;

import JdbcTemplate.config.Config;
import JdbcTemplate.operation.query.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcOperations;

public class Query1 {

    private static Person person;

    public static Person operation(int id){

        //查询语句
        String query="SELECT * FROM spitter WHERE id_identity=?";

        ApplicationContext configApplicationContext=new AnnotationConfigApplicationContext(Config.class);

        //获取JdbcOperations向下转型JdbcTemplate的对象
        JdbcOperations jdbcOperations= (JdbcOperations) configApplicationContext.getBean("jdbcOperations");

        //使用Lambda表达式
        /*
        queryForObject返回值为<T>,内部返回Person类型时<Person>,故用Person对象接收
        内层return返回Person,外层return也返回Person
        (rs,rowNum)为接口唯一方法的参数
         */
        return
                person=jdbcOperations.queryForObject(query,(rs,rowNum)->{
            return
                   new Person(
                            rs.getInt("id_identity"),
                            rs.getString("username"),
                            rs.getString("password"),
                            rs.getString("first_name"),
                            rs.getString("last_name"),
                            rs.getString("email"));
        },id);//不使用预处理语句去掉该参数即可/
    }

    public static void main(String[] args) {

        operation(7);
        System.out.println(
                        person.getFirst_name()+
                        person.getFirst_name()+
                        person.getLast_name()+
                        person.getEmail()+
                        person.getPassword()+
                        person.getId_identity()
        );
    }
}
